<template>
  <el-dialog
    :title="!dataForm.apprId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="病情描述" prop="patientState">
      <el-input v-model="dataForm.patientState" placeholder="病情描述"></el-input>
    </el-form-item>
    <el-form-item label="评价内容" prop="apprContent">
      <el-input v-model="dataForm.apprContent" placeholder="评价内容"></el-input>
    </el-form-item>
    <el-form-item label="星评" prop="apprRank">
      <el-input v-model="dataForm.apprRank" placeholder="星评"></el-input>
    </el-form-item>
    <el-form-item label="诊断记录外键id" prop="diagId">
      <el-input v-model="dataForm.diagId" placeholder="诊断记录外键id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          apprId: 0,
          patientState: '',
          apprContent: '',
          apprRank: '',
          diagId: ''
        },
        dataRule: {
          patientState: [
            { required: true, message: '病情描述不能为空', trigger: 'blur' }
          ],
          apprContent: [
            { required: true, message: '评价内容不能为空', trigger: 'blur' }
          ],
          apprRank: [
            { required: true, message: '星评不能为空', trigger: 'blur' }
          ],
          diagId: [
            { required: true, message: '诊断记录外键id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.apprId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.apprId) {
            this.$http({
              url: this.$http.adornUrl(`/aftersystem/appraise/info/${this.dataForm.apprId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.patientState = data.appraise.patientState
                this.dataForm.apprContent = data.appraise.apprContent
                this.dataForm.apprRank = data.appraise.apprRank
                this.dataForm.diagId = data.appraise.diagId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/aftersystem/appraise/${!this.dataForm.apprId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'apprId': this.dataForm.apprId || undefined,
                'patientState': this.dataForm.patientState,
                'apprContent': this.dataForm.apprContent,
                'apprRank': this.dataForm.apprRank,
                'diagId': this.dataForm.diagId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
